package thss.mro.simulate;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import thss.mro.entity.Packet;
import thss.mro.entity.PacketField;
import thss.mro.entity.TemplateField;
import thss.mro.entity.template.ProtocolTemplate;
import thss.mro.util.Constants;

/**
 * 抽象类：模拟数据生成器
 */
public abstract class Generator {

//	public Packet pack = null;//为便于查看生成的模拟数据
	
	/**
	 * 获得模拟数据包数据
	 */
	public abstract byte[] getData();
	
	/**
	 * 生成数据
	 */
	@SuppressWarnings("unchecked")
	protected byte[] generateData(ProtocolTemplate pt){
//		pack.setType(pt.getType());
		if(pt.getValueType()==Constants.PROTOCOL_VALUE_TYPE_BIT){//单位为位时直接转换为(位数/8)个随机字节
			int length = pt.getFieldChain().size();
			int byteLen = length/8;
			return getRandomByteStream(byteLen);
		}
		byte[] data = new byte[300];
		LinkedHashMap<String,TemplateField> fieldChain = pt.getFieldChain();
		int offset = 0;
		Map.Entry entry = null;
		Iterator it = fieldChain.entrySet().iterator();
		//循环为协议各字段生成随机数据
		while (it.hasNext()){ 
			entry = (Map.Entry)it.next();
			TemplateField profd = (TemplateField)entry.getValue();
			PacketField packfd = new PacketField();
			packfd.setTemplateField(profd);
			if(profd.isDynamic()){ //动态字段时候(暂不考虑对动态字段的生成)
//				int num = pack.getUnfixedFieldMap().get(profd.getName());
//				for(int j=0;j<num;j++){
////					PacketField subPackfd = new PacketField();
////					subPackfd.setProtocolField(profd);
//					byte[] value = getRandomByteStream(packfd);
//					System.arraycopy(value,0,data,offset,profd.getLength());
//					offset += profd.getLength();
////					subPackfd.setValue(value);
//					packfd.getValueLst().add(value);
//				}
			}else{
				byte[] value = getRandomByteStream(packfd);
				System.arraycopy(value,0,data,offset,profd.getLength());
				offset += profd.getLength();
				packfd.setTemplateField(profd);
				packfd.setValue(value);
//				pack.getFieldChain().put(profd.getName(), packfd);
			}
//			pack.addField(packfd);
		}
		byte[] rst = new byte[offset];
		System.arraycopy(data, 0, rst, 0, offset);
		return rst;
	}
	
	/**
	 * 根据协议字段数据类型与长度随机生成字节流
	 * @param fieldName	-	字段名（对某些字段做特殊处理时用到）
	 * @param type		-	字段类型
	 * @param num		-	字段长
	 * @return
	 */
	protected byte[] getRandomByteStream(PacketField packFd){
		Random random = new Random();
		TemplateField proFd = packFd.getTemplateField();
		byte[] rst = new byte[proFd.getLength()];
		switch(proFd.getType()){
			case Constants.PROTOCOL_FIELD_TYPE_CHAR:
				for(int i=0;i<proFd.getLength();i++)
					rst[i] = (byte)('a'+random.nextInt(26));
				break;
//			case Constants.PROTOCOL_FIELD_TYPE_CELL:
//				return new CellGenerator().getData();
			default: //默认产生Byte型随机数据
				random.nextBytes(rst);
				break;
		}
		return rst;
	}
	
	protected byte[] getRandomByteStream(int length){
		byte[] rst = new byte[length];
		new Random().nextBytes(rst);
		return rst;
	}
}
